/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Nov 13, 2008

package com.sceneworker.editors.beanshelleditor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import bsh.EvalError;
import bsh.Interpreter;
import bsh.NameSpace;
import bsh.This;

import com.jme.scene.Controller;
import com.jme.scene.Spatial;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;


public class BeanShellController extends Controller{

	public static final String DEFAULT_SCRIPT = "// the update method is called on every update of the controller\n// the object parameter is the spatial that the controller is attached to"
				 + "\n\nupdate(object) {\n}";
	private		String		mstr_script;
	private		Spatial		mcl_spatial;
	private		String		mstr_scriptLocation;
	
	private		Interpreter		mcl_interpreter;
	private		This			mcl_this;
	
	public BeanShellController() {
		mstr_script = DEFAULT_SCRIPT;
		evaluateScript();
	}
	
	public	void	setScript(String str_script) {
		mstr_script = str_script;
		
		evaluateScript();
	}
	
	public	String	getScript() {
		return mstr_script;
	}
	
	public	void	setSpatial(Spatial cl_spatial) {
		mcl_spatial = cl_spatial;
	}
	
	public	Spatial		getSpatial() {
		return mcl_spatial;
	}
	
	public	void	setScriptLocation(String str_scriptLocation) {
		File		lcl_f = new File(str_scriptLocation);
		try {
			BufferedReader		lcl_in = new BufferedReader(new FileReader(lcl_f));
			
			StringBuffer		lcl_script = new StringBuffer();
			
			String		lstr_line = lcl_in.readLine();
			
			while(lstr_line != null) {
				lcl_script.append(lstr_line + "\n");
				lstr_line = lcl_in.readLine();
			}
			
			lcl_in.close();
			
			mstr_script = lcl_script.toString();
			
			mstr_scriptLocation = str_scriptLocation;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return;
		}
		
		evaluateScript();
	}
	
	private		void	evaluateScript() {
		try {
			mcl_interpreter = new Interpreter();
			Object		lcl_temp = mcl_interpreter.eval(mstr_script);
			NameSpace	lcl_nm = mcl_interpreter.getNameSpace();
			
			String		lstra_temp[] = lcl_nm.getMethodNames();
			
		} catch (EvalError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void update(float time) {
		if(mstr_script == null) {
			return;
		}
		
		try {
			NameSpace	lcl_nm = mcl_interpreter.getNameSpace();
			lcl_nm.invokeMethod("update", new Object[] { mcl_spatial }, mcl_interpreter);
			
		} catch (EvalError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void write(JMEExporter e) throws IOException {
        OutputCapsule capsule = e.getCapsule(this);
        capsule.write(mstr_script, "script", DEFAULT_SCRIPT);
        capsule.write(mstr_scriptLocation, "scriptLocation", null);
    }
    
    public void read(JMEImporter e) throws IOException {
        InputCapsule capsule = e.getCapsule(this);
        mstr_script = capsule.readString("script", DEFAULT_SCRIPT);
        mstr_scriptLocation = capsule.readString("scriptLocation", null);
        
        evaluateScript();
    }
    
    public Class getClassTag() {
        return this.getClass();
    }

}
